package com.starinside.wp.controller.api;

import com.starinside.wp.domain.WxUser;
import com.starinside.wp.foundation.model.ErrorCode;
import com.starinside.wp.foundation.model.StatusResponse;
import com.starinside.wp.foundation.query.base.PagerQuery;
import com.starinside.wp.service.AppUserService;
import com.starinside.wp.service.TeachAPICallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

import static com.starinside.wp.foundation.constant.RequestConstant.TICKET;


@RestController
@RequestMapping("/wx/api/complaintRecord")
@Api(value = "/wx/api/complaintRecord", tags = "投诉")
@Slf4j
public class APIComplaintRecordController {

    @Autowired
    private TeachAPICallService teachAPICallService;

    @Autowired
    private AppUserService appUserService;


    /**微信端
     * 新增投诉与建议
     * @param complaintObject
     * @param title
     * @param content
     * @param files 图片文件列表。可以传多个图片。多个图片已"|"分隔
     * @return
     */
    @ApiOperation("增加投诉")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "complaintObject", value = "投诉岗位 Code", example = "ROLE_BUSINESS"),
            @ApiImplicitParam(name = "title", value = "标题"),
            @ApiImplicitParam(name = "content", value = "内容"),
    })
    @RequestMapping(value = "/addRecord", method = RequestMethod.POST)
    public StatusResponse addComplaintRecord(@RequestHeader(TICKET) String ticket,
                                             HttpServletRequest req, HttpServletResponse resp,
                                             String complaintObject, String title, String content,
                                             String files){

        WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");

        if(StringUtils.isEmpty(loginUser.getDefaultBabyId())){
            return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST, "木找到宝宝");
        }
                                                                            //(String wxUserId, String usersId, String childrenId, String complaintObject, String title, String content, MultipartFile[] files);

//        MultipartFile[] files = {file};

        Map<String, Object> record = teachAPICallService.addComplaintRecord(loginUser.getId(), loginUser.getParentMobile(), loginUser.getDefaultBabyId(), complaintObject, title, content, files);
        return StatusResponse.success(record);
    }

    /**微信端
     * 查询家长的投诉列表
     * @param query
     * @return
     */
    @RequestMapping(value = "/getRecords", method = RequestMethod.GET)
    @ApiOperation("查询投诉列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码"),
            @ApiImplicitParam(name = "pageSize", value = "页容量")
    })
    public StatusResponse getComplaintRecord(@RequestHeader(TICKET) String ticket,
                                             HttpServletRequest req, HttpServletResponse resp, PagerQuery query){
        WxUser loginUser = (WxUser) req.getSession().getAttribute("loginUser");

        if(StringUtils.isEmpty(loginUser.getDefaultBabyId())){
            return StatusResponse.error(ErrorCode.CHILDREN_NOT_EXIST, "木找到宝宝");
        }

        if(StringUtils.isEmpty(loginUser.getParentMobile())){
            return StatusResponse.error(ErrorCode.MOBILE_IS_EXIST, "手机号为空");
        }

        if(StringUtils.isEmpty(loginUser.getParentId())){
            return StatusResponse.error(ErrorCode.GENRAL_INTERNAL_ERROR, "意料之外的错误");
        }

        Map ret = teachAPICallService.getComplaintRecord(loginUser.getParentId(), query.getPageNum(), query.getPageSize());
        return StatusResponse.success(ret);
    }

}
